/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package backend.helper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * HelperClass providing some static methods which can be accessed from everywhere 
 * 
 * @author Markus Moormann
 */
public class HelperClass {
	
	/**
	 * a logger to log some information
	 */
	private static Logger LOG = Logger.getLogger(HelperClass.class);
	/**
	 * the dateFormat containing the format for MySQL date 
	 */
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/**
	 * contains the properties 
	 */
	private static Properties prop = new Properties();
	/**
	 * filename pointing to the properties.xml
	 */
	private static String filename = HelperClass.class.getResource("/../../").getPath() + "properties.xml";
	
	/**
	 * load properties into <code>prop</code>
	 */
	private static void loadProperties() {
		try {
			prop.loadFromXML(new FileInputStream(filename));
		} catch (InvalidPropertiesFormatException e) {
			LOG.error(e);
		} catch (FileNotFoundException e) {
			LOG.error(e);
		} catch (IOException e) {
			LOG.error(e);
		}
	}
	
	/**
	 * save properties to XML file
	 */
	private static void saveProperties() {
		try {
			prop.storeToXML(new FileOutputStream(filename), new Date().toString());
		} catch (FileNotFoundException e) {
			LOG.error(e);
		} catch (IOException e) {
			LOG.error(e);
		}
	}

	/**
	 * set a property in properties
	 * 
	 * @param key key of property
	 * @param value value of property
	 */
	public static void setProperty(String key, String value) {
		loadProperties();
		prop.setProperty(key, value);
		saveProperties();
	}
	
	/**
	 * get the value to a key
	 * 
	 * @param key the key to get the value of
	 * @return the value
	 */
	public static String getProperty(String key) {
		loadProperties();
		return prop.getProperty(key);
	}
	
	/**
	 * format a date by following format yyyy-MM-dd HH:mm:ss (sql)
	 * 
	 * @param date the date to format
	 * @return the formated date
	 */
	public static String format(Date date) {
		return dateFormat.format(date);
	}
	
	/**
	 * rebuilds the php function implode
	 * for further information have a look at
	 * <a href="http://de.php.net/manual/de/function.implode.php">implode</a>
	 * 
	 * @param ary the array to concat
	 * @param delim the delimiter which should be between the array pieces
	 * @return gives back a string that represent all array elements in their order seperated by <code>delim</code>
	 */
	public static String implode(Object[] ary, String delim) {
	    String out = "";
	    for(int i=0; i<ary.length; i++) {
	        if(i!=0) { out += delim; }
	        out += ary[i].toString();
	    }
	    return out;
	}

	public static String getExtension(File slide) {
		int dot = slide.getAbsolutePath().lastIndexOf(".");
	    return slide.getAbsolutePath().substring(dot + 1);
	}

}
